//
//  HTCFQuickCacheUitil.m
//  HTHappySale
//
//  Created by 创维海通 on 2017/9/15.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "HTCFQuickCacheUitil.h"
#import "HTCFQuickCacheUitilMacrocDefine.h"

@implementation HTCFQuickCacheUitil


+ (BOOL)checkValue:(id)value key:(id)key{
    if(HTCFCheckObjectNull(value)||HTCFCheckObjectNull(key)){
        return YES;
    }else{
        return NO;
    }
}


+ (BOOL)isNullObject:(id)anObject{
    if (!anObject || [anObject isKindOfClass:[NSNull class]]) {
        return YES;
    }else{
        return NO;
    }
}

+ (NSUserDefaults *)standDefault
{
    return [NSUserDefaults standardUserDefaults];
}

+ (void)userDefaultCache:(id<NSCoding>)value key:(id)key{
    if (HTCFCheckKeyValueHasNull(key, value)) {
        return;
    }
    [[HTCFQuickCacheUitil standDefault]setObject:value forKey:key];
}


+ (void)userDefaultRemove:(id)key{
    if (HTCFCheckObjectNull(key)) {
        return;
    }
    [[HTCFQuickCacheUitil standDefault]removeObjectForKey:key];
}


+ (id)userDefaultGetValue:(id)key{
    if (HTCFCheckObjectNull(key)) {
        return nil;
    }
    return [[HTCFQuickCacheUitil standDefault]objectForKey:key];
}


+ (BOOL)userDefaultEmptyValue:(id)key{
    return [HTCFQuickCacheUitil userDefaultGetValue:key] == nil;
}

/**
 *  获取系统默认文件管理
 */
+ (NSFileManager *)fileManager;
{
    return [NSFileManager defaultManager];
}

/**
 *  指定路径pathObj是否存在文件
 */
+ (BOOL)fileExist:(NSString*)path
{
    if (HTCFStringIsNull(path)) {
        return NO;
    }
    return [[HTCFQuickCacheUitil fileManager] fileExistsAtPath:path];
}

/**
 *  指定路径pathObj是否存在目录
 */
+ (BOOL)directoryExist:(NSString*)dirPath
{
    if (HTCFStringIsNull(dirPath)) {
        return NO;
    }
    BOOL isDir = YES;
    
    return [[HTCFQuickCacheUitil fileManager] fileExistsAtPath:dirPath isDirectory:&isDir];
}

/**
 *  读取某个路径的二进制数据，返回 NSData
 */
+ (NSData *)readFromFile:(NSString *)path{
    if (HTCFCheckObjectNull(path)) {
        return nil;
    }
    
    return [NSData dataWithContentsOfFile:path];
}

/**
 *  将二进制数据写入文件 dataObj:NSData pathObj:NSString
 */
+ (BOOL)writeFileData:(NSData*)data toPath:(NSString *)path{
    if (HTCFCheckKeyValueHasNull(data, path)) {
        return NO;
    }
    
    return [data writeToFile:path atomically:YES];
}

/**
 *  在指定路径创建目录，返回BOOL结果
 */
+ (BOOL)createDirectory:(NSString*)dirPath
{
    
    if (HTCFCheckObjectNull(dirPath)) {
        return NO;
    }
    if (HTCFFileDirectoryIsExist(dirPath)) {
        return YES;
    }
    return [[HTCFQuickCacheUitil fileManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];
}

/**
 *  删除指定路径文件
 */
+ (BOOL)deleteFileAtPath:(NSString *)filePath
{
    if (HTCFStringIsNull(filePath)) {
        return NO;
    }
    return [[HTCFQuickCacheUitil fileManager] removeItemAtPath:filePath error:nil];
}

/**
 *  删除指定目录
 */
+ (BOOL)deleteDirectoryAtPath:(NSString *)dirPath
{
    if (HTCFStringIsNull(dirPath)) {
        return NO;
    }
    return [[HTCFQuickCacheUitil fileManager] removeItemAtPath:dirPath error:nil];
}

/**
 *  从指定路径读取被归档过的对象
 */
+ (id)unarchieveFromPath:(NSString *)filePath
{
    if (HTCFStringIsNull(filePath)) {
        return nil;
    }
    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

/**
 *  获取NSDocumentDirectory目录
 */
+ (NSString *)documentDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
}

/**
 *  获取NSCacheDirectory目录
 */
+ (NSString *)cacheDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
}

/**
 *  返回文件名为fileName在NSDocumentDirectory中的路径
 */
+ (NSString *)documentDirectoryPath:(NSString *)file
{
    if (HTCFStringIsNull(file)) {
        return nil;
    }
    return [[HTCFQuickCacheUitil documentDirectory]stringByAppendingPathComponent:file];
}

/**
 *  返回文件名为fileName在NSCacheDirectory中的路径
 */
+ (NSString *)cacheDirectoryPath:(NSString *)file
{
    if (HTCFStringIsNull(file)) {
        return nil;
    }
    return [[HTCFQuickCacheUitil cacheDirectory]stringByAppendingPathComponent:file];
}

@end
